﻿@{
    ViewBag.Title = "MemberShare";
}
@section scripts
{  
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script> 
}
   <div class="mini-splitter" style="width:100%;height:100%;">
    <div size="400" showcollapseButton="true">
        @Html.Partial("~/Views/Modular/ToolbarType.cshtml")
        <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
            <table>
                <tr>
                    <td width ="80px" align="right">编码或名称: </td>
                    <td width ="110px" ><input class="mini-textbox" id ="FSearchKey" width ="110px" name = "FSearchKey"  onkeypress="setPress()"/></td>
                    <td width ="80px" align="center">
                        <a class="mini-button" iconCls="icon-search" onclick="LoadAgentList" plain="true">搜索</a>
                    </td>
                </tr>
            </table>
        </div>
        <div class="mini-fit">
                    <div id="datagrid2" class="mini-datagrid" style="width:100%;height:100%;" pageSize="15"
                        borderStyle="border:1;" url="GetGroup"
                        allowCellSelect="true" allowCellEdit="true"showColumnsMenu="true"fitColumns="false"  
                        onrowdblclick="editClass"  sortMode="client" onselectionchanged="LoadListData">
                    <div property="columns">
                        <div type="checkcolumn" >选行</div>
                        <div type="indexcolumn" width="40" headerAlign="center" >序号</div>
                        <div field="FNumber" name="FNumber" width="80" headeralign="center" allowsort="true">共享组编码</div>
                        <div field="FName" name="FName" width="100" headeralign="center" allowsort="true">共享组名称</div>
                        <div field="FRemark" name="FRemark" width="120" headeralign="center" allowsort="true">备注</div>
                    </div>
                </div>
        </div>
    </div>
       
    <div showCollapseButton="true">
        <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
            <table style="width:100%;">
                <tr>
                    <td style="width:100%;">
                        <a class="mini-button" id="fresh" name="fresh" iconCls="icon-search" onclick="search" plain="true">刷新</a>                         
                        <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="newBill" plain="true" visible="@ViewBag.add">新增</a>
                        <a class="mini-button" id="save" name="save" iconCls="icon-save" onclick="saveas" plain="true" visible="@ViewBag.save">保存</a>
                        <a class="mini-button" id="delrow" name="delrow" iconCls="icon-remove" onclick="removeRows" plain="true" visible="@ViewBag.remove">删除</a>
                         <span class="separator"></span>
                        <a class="mini-button" id="close" name="close" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                    </td>
                </tr>
            </table>       
        </div>
        <table>
            <tr>
                <td width ="100px" align="right">编码或名称: </td>
                <td width ="150px" ><input class="mini-textbox" width ="150px" id ="FSearchKeyS" name = "FSearchKeyS" onkeypress="setkeyPress()"/></td>
                <td width ="100px" align="center">
                    <a class="mini-button" iconCls="icon-search" onclick="search(0)" plain="true">查询</a>
                </td>
            </tr>
        </table>
        <div class="mini-fit" >
            <div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;" pageSize="15"
                    borderStyle="border:1;" url="../Member/GroupDetailList" oncellcommitedit="onCellCommitEdit"
                    allowCellSelect="true" allowCellEdit="true" showColumnsMenu="true" fitColumns="false"  
                    onrowdblclick="modify"  sortMode="client"multiSelect="true">
                <div property="columns">
                    <div type="checkcolumn" ></div>
                    <div type="indexcolumn" width="40" headerAlign="center" >序号</div>
                    <div field="FNumber" name="FNumber" width="100" headeralign="center" allowsort="true">商户编码</div>
                    <div field="FName" name="FName" width="150" headeralign="center" allowsort="true">商户名称</div>
                    <div field="FManager" name="FManage" width="100" headeralign="center" allowsort="true">联系人</div>
                    <div field="FTelephone" name="FTelephone" width="100" headeralign="center" allowsort="true">联系电话</div>
                    <div field="FShareFlag" type="checkboxcolumn" name="FShareFlag" trueValue="1" falseValue="0" width="60" headeralign="center" allowsort="true">礼券共享</div>
                    <div field="FTimeShareFlag" type="checkboxcolumn" name="FTimeShareFlag" trueValue="1" falseValue="0" width="90" headeralign="center" allowsort="true">次卡资料共享</div>
                    <div field="FRemark" name="FRemark" width="120" headeralign="center" allowsort="true">备注</div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    mini.parse();
    var grid = mini.get("datagrid1");
    var grid2 = mini.get("datagrid2");

    var sMergeFlag = 0;

    function FormShown() {
        LoadAgentList();
        LoadListData(0);
    }
    function setkeyPress() {
        LoadListData(0);
    }
    function setPress() {
        LoadAgentList();
    }
    function LoadListTree() {
        LoadAgentList();
        grid.setData(null);
    }
    function LoadAgentList() {
        var json = "[{'FSystemAppID':'"+parent.window.getSystemAppID()+"',";
        json = json + "'search':" + "'" + mini.getbyName("FSearchKey").getValue() + "'";
        json = json + "}]";
        grid2.load({ mjson: json });
    }


    //加载资料列表
    function LoadListData(mdatatype) {
        mini.get("save").setEnabled(false);
        var sSystemAppID = parent.window.getSystemAppID();
        var node = grid2.getSelected()
        if (node) {
            var classid = node.FGroupID;
            var json = "[{";
            json = json + "'FSystemAppID':" + "'" + sSystemAppID + "',";
            json = json + "'mClassID':" + "'" + classid + "',";
            json = json + "'mNumber':" + "'" + mini.getbyName("FSearchKeyS").getValue() + "',";
            json = json + "'mDatatype':" + "'" + mdatatype + "'";
            json = json + "}]";
            grid.load({ mydata: json });

            mExportDataUrl = grid.url;
            mExportDataJson = json;
        }
    }

    //搜索资料列表
    function search(mdatatype) {
        LoadListData(mdatatype);
    }

    ///增加分类
    function addClass() {
        var node = grid2.getSelected()
        mini.open({
            url: "../Member/GroupInfo",
            title: "会员共享组", width: 500, height: 300,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new"};
                iframe.contentWindow.SetInfoData(data);
            },
            ondestroy: function (action) {
                grid2.reload();
            }
        });
    }

    //修改分类
    function editClass() {
        var node = grid2.getSelected()
        if (node) {
            if (node.FItemID < 0)
                return;
            mini.open({
                url: "../Member/GroupInfo",
                title: "会员共享组", width: 500, height: 300,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { action: "edit", FItemID: node.FItemID, FClassID: node.FItemID, FClassNumber: node.FNumber, FClassName: node.FName, FParentID: node.FParentID };
                    iframe.contentWindow.SetInfoData(data);
                },
                ondestroy: function (action) {
                    grid2.reload();
                }
            });
        }
    }

    //删除分类
    function removeClass() {
        if (grid.getData().length > 0) {
            mini.alert("该组有资料存在，不能删除！ ");
            return;
        }
        var node = grid2.getSelected()
        if (node) {
            var FSystemAppID = parent.window.getSystemAppID();
            if (node.FItemID > 0) {
                mini.confirm("确定要删除选中的记录吗？", "提示",
                    function (action) {
                        if (action == "ok") {
                            var json = "{'FItemID':'" + node.FItemID + "'}";
                            $.post("../Member/delGroup", { json: json, mSystemAppID: FSystemAppID }, function (data) {
                                var ret = mini.decode(data);
                                if (ret.status == 1) {
                                    grid2.reload();
                                    mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                                }
                                else {
                                    mini.alert("异常:" + ret.error);
                                }
                            });
                        }
                    });
            }
        }
        else {
            mini.alert("请选中一条记录");
        }
    }



    function newBill() {
        var uID = parent.window.getSystemAppID();
        var node = grid2.getSelected();
        if (!node)
        {
            mini.alert("请选择一个组再新增");
            return;
        }
        if (node.FGroupID == 0) {
            mini.alert("请选择一个组再新增");
            return;
        }
        mini.open({
            url: "/GetItem/Index",
            title: "选择商户添加到共享组", width: 700, height: 470,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new", sTbName: "MemberGroupSubCorp", sSearchKey: '', ClassID: node.FGroupID };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action) {
                    var json = "[";
                    for (var i = 0; i < action.length; i++) {
                        if (i > 0) json = json + ',';
                        js = '{"FItemID":' + action[i].FItemID + '}';
                        json = json + js
                    }
                    json = json + "]";
                    if (json.length < 8) { return; }
                    AddGroup(json, node);
                }
            }
        });
    }

    function AddGroup(json, node) {
        $.post("SaveGroupDetail", { json: json, ClassID: node.FGroupID, mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: '新增成功', state: "success", x: "center", y: "top", timeout: 3000 });
                grid.reload();
            }
            else {
                //mini.alert("错误信息:" + ret.error);
                if (sMergeFlag == 0) {
                    ConfirmGroup(json, node);
                } else {
                    Merge(json, node);
                }
            }
        });
    }

    function Merge(json, node) {
        sMergeFlag = 1;
        var data = mini.decode(json);
        var jsonstr = '[{"mSystemAppID":"' + parent.window.getSystemAppID() + '",'
            + '"FGroupID":' + node.FGroupID + ','
            + '"FCorpID":' + data[0].FItemID + '}]';
        $.post("Merge", { json: jsonstr }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                AddGroup(json, node);
            }
            else {
                mini.alert(ret.error);
            }
        });
    }

    function ConfirmGroup(json, node) {
        mini.open({
            url: "../Member/Confirm",
            title: "加入会员共享提示", width: 400, height: 300,
            onload: function () {                
            },
            ondestroy: function (action) {
                if (action == 1) {
                    Merge(json, node);
                }
            }
        });
    }

    //删除资料
    function removeRows() {
        var row = grid.getSelected();
        if (row) {
            mini.confirm("确定要删除选中的记录吗？", "提示",
                function (action) {
                    if (action == "ok") {
                        SetRemoveRows();
                    }
                });
        } else {
            mini.alert("请选中一条记录再删除！");
        }
    }

    function SetRemoveRows() {
        var FSystemAppID = parent.window.getSystemAppID();
        var rows = grid.getSelecteds();
        var json = "[";
        for (var i = 0; i < rows.length; i++) {
            if (i > 0)
                json = json + ',';
            js = '{"FItemID":' + rows[i].FItemID + '}';
            json = json + js
        }
        json = json + "]";
        if (json.length < 8) {
            mini.alert("没有要保存的数据！");
            return;
        }
        $.post("RemoveMGroupDetail", { json: json, mSystemAppID: FSystemAppID }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                grid.reload();
            }
            else {
                mini.alert("异常:" + ret.error);
            }
        });
    }

    function saveas() {
        var rows = grid.getChanges();
        var json = mini.encode(rows);
        $.post("SaveMemberGroupBatch", { json: json, mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: '保存成功', state: "success", x: "center", y: "top", timeout: 3000 });
                LoadListData(0);
            }
            else {
                mini.showTips({ content: '保存失败 ' + ret.error, state: "danger", x: "center", y: "top", timeout: 3000 });
            }
        });
    }

    function onCellCommitEdit(e) {
        mini.get("save").setEnabled(true);//编辑后保存按钮可用
    }

</script>